package com.bsu.ascm.operation.base;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import com.bsu.ascm.Ascm;
import com.bsu.ascm.http.AscmHttpClient;

public class OperationBase extends DefaultHandler{
	protected Ascm activity = null;
	
	protected AscmHttpClient http = new AscmHttpClient();				//用来向服务器请求数据的对象
	
	private SAXParserFactory factory = null;
	private SAXParser parser = null;
	private XMLReader xmlreader = null;
	protected StringBuilder currentMessage = new StringBuilder();
	/**
	 * 构造函数，作一些初始化工作
	 * @param a
	 */
	public OperationBase(Ascm a){
		activity = a;
		try{
			factory = SAXParserFactory.newInstance();
			parser = factory.newSAXParser();
			xmlreader = parser.getXMLReader();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} 
	}
	/**
	 * 开始解析代入的xml数据
	 * @param xml 代入xml数据
	 */
	protected void parseXml(String xml){
		try{
			StringReader sr = new StringReader(xml);
			InputSource is = new InputSource(sr);
			xmlreader.setContentHandler(this);
			xmlreader.parse(is); 
		} catch(SAXException e){
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}
		
	}
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		currentMessage.delete(0, currentMessage.length());
		currentMessage.append(ch,start,length);
	}
	/**
	 * 设置layout
	 * @param layoutResID
	 */
	public void setContents(int layoutResID){
		activity.setContentView(layoutResID);
//		if(!isInit){
			init();
//			isInit = true;
//		}
	}
	/**
	 * 初始化工作
	 */
	protected void init(){
		
	}
}
